// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.mail.gui.action;

import java.awt.event.ActionEvent;
import java.util.Observable;
import java.util.Observer;

import org.columa.core.config.IDefaultItem;
import org.columba.api.gui.frame.IFrameMediator;
import org.columba.core.config.DefaultItem;
import org.columba.core.config.ViewItem;
import org.columba.core.gui.action.AbstractSelectableAction;
import org.columba.core.gui.frame.DefaultFrameController;
import org.columba.core.xml.XmlElement;
import org.columba.mail.gui.frame.ThreePaneMailFrameController;

/**
 * Show/Hide message previous window.
 * 
 * @author fdietz
 *  
 */
public class ShowMessagePreviewAction extends AbstractSelectableAction
		implements Observer {

	private XmlElement element;

	/**
	 * @param controller
	 */
	public ShowMessagePreviewAction(IFrameMediator controller) {
		super(controller, "Message Preview");

		ViewItem item = ((DefaultFrameController)frameMediator).getViewItem();
				
		element = item.getRoot().getElement("splitpanes");
		if ( element == null ) {
			element = new XmlElement("splitpanes");
			item.getRoot().addElement(element);
		}
		
		element.addObserver(this);

		update(element, null);

	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent arg0) {

		
		((ThreePaneMailFrameController) frameMediator)
				.enableMessagePreview(getState());
		
		//DefaultItem item = new DefaultItem(element);
		//item.set("header_enabled", getState());
		
	}

	/**
	 * Method is called when configuration changes.
	 * 
	 * @param arg0
	 * @param arg1
	 */
	public void update(Observable arg0, Object arg1) {

		IDefaultItem item = new DefaultItem(element);
		boolean enabled = item.getBooleanWithDefault("header_enabled", true);
		
		setState(enabled);
	}
}